﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Noris.LCS.AIDCCodes.Test
{
    public partial class TestForm : Form
    {
        public TestForm()
        {
            InitializeComponent();
            _QrMessagesInit();
        }
        private void TestForm_Load(object sender, EventArgs e)
        {
            this._CodeTypeTab.SelectedIndex = 0;

            string text = "http://www.yahoo.com/";
            if (Environment.UserDomainName == "ASOL")
            {
                if (Environment.UserName == "David.Janacek")
                    text = "mailto:daj@helios.cz";
                else if (Environment.UserName == "Radek.Klasterka")
                    text = "https://www.yammer.com/helios.eu";
                else
                    text = "http://www.helios.eu/cz.html";
            }
            this._QrCodeText.Text = text;

            this._CorrectLevelCombo.SelectedIndex = 1;
            this._ModuleSize = 6;
            this._SaveAsDir = System.Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory); // System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
            this._CurrentQrCodeType = CodeType.QrCodeMedium;

            this._BarCodeText.Text = "8594003840173";
            this._InitBarCodeTypes();
            this._CurrentBarCodeType = CodeType.EAN13;

            this._ToolLabelBtn.Checked = true;
            this._CurrentLabelVisible = true;
            this._StatusPatternDdb.Visible = false;
            this._InitEvents();

            this.ShowCodeControl(true);
        }
        private void _InitEvents()
        {
            // PAGE selector:
            this._CodeTypeTab.SelectedIndexChanged += new System.EventHandler(this._CodeTypeTab_SelectedIndexChanged);

            // QR code page events:
            this._QrCodeText.TextChanged += this._QrCodeText_TextChanged;
            this._CorrectLevelCombo.SelectedIndexChanged += this._CorrectLevelCombo_SelectedIndexChanged;
            
            // QR message events:
            this._PhoneNumber.TextChanged += this._PhoneNumber_TextChanged;
            this._SmsPhoneNumber.TextChanged += this._SmsPhoneNumber_TextChanged;
            this._SmsContent.TextChanged += this._SmsContent_TextChanged;
            this._WebUrl.TextChanged += this._WebUrl_TextChanged;
            this._MailAdresss.TextChanged += this._MailAdresss_TextChanged;
            this._MailSubject.TextChanged += this._MailSubject_TextChanged;
            this._MailContent.TextChanged += this._MailContent_TextChanged;
            this._ContactFirstName.TextChanged += this._ContactFirstName_TextChanged;
            this._ContactLastName.TextChanged += this._ContactLastName_TextChanged;
            this._ContactAdressLine1.TextChanged += this._ContactAdressLine1_TextChanged;
            this._ContactAdressLine2.TextChanged += this._ContactAdressLine2_TextChanged;
            this._ContactAdressCity.TextChanged += this._ContactAdressCity_TextChanged;
            this._ContactAdressPostCode.TextChanged += this._ContactAdressPostCode_TextChanged;
            this._ContactPhone1.TextChanged += this._ContactPhone1_TextChanged;
            this._ContactPhone2.TextChanged += this._ContactPhone2_TextChanged;
            this._ContactPhone3.TextChanged += this._ContactPhone3_TextChanged;
            this._ContactEMail.TextChanged += this._ContactEMail_TextChanged;

            this._SpdSAccountPrefix.TextChanged += this._SpdSAccountPrefix_TextChanged;
            this._SpdSAccountNumber.TextChanged += this._SpdSAccountNumber_TextChanged;
            this._SpdSBankCode.TextChanged += this._SpdSBankCode_TextChanged;
            this._SpdAccountIban.TextChanged += this._SpdAccountIban_TextChanged;
            this._SpdAmount.TextChanged += this._SpdAmount_TextChanged;
            this._SpdCurrencyCode.TextChanged += this._SpdCurrencyCode_TextChanged;
            this._SpdDueDate.TextChanged += this._SpdDueDate_TextChanged;
            this._SpdVarSymb.TextChanged += this._SpdVarSymb_TextChanged;
            this._SpdSpecSymb.TextChanged += this._SpdSpecSymb_TextChanged;
            this._SpdConstSymb.TextChanged += this._SpdConstSymb_TextChanged;
            this._SpdMessage.TextChanged += this._SpdMessage_TextChanged;
            this._SpdRecIdent.TextChanged += this._SpdRecIdent_TextChanged;
            this._SpdRecName.TextChanged += this._SpdRecName_TextChanged;
            this._SpdPayIdent.TextChanged += this._SpdPayIdent_TextChanged;
            this._SpdPayType.TextChanged += this._SpdPayType_TextChanged;

            // BAR code page events:
            this._BarCodeText.TextChanged += new EventHandler(_BarCodeContent_TextChanged);

            // CODE events:
            this._CodeControl.TimeChanged += this._QControl_TimeChanged;
        }
        private void _CodeTypeTab_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.ShowCodeControl(true);
        }
        private void ShowCodeControl(bool withRefresh)
        {
            int codePage = this._CodeTypeTab.SelectedIndex;
            this._IsQrCode = (codePage == 0);
            this._IsBarCode = (codePage == 1);
            bool isCode = (this._IsBarCode || this._IsQrCode);
            this._CodeControl.Visible = isCode;

            this._CodeControl.Dock = (isCode ? DockStyle.Fill : DockStyle.None);

            if (this._IsBarCode)
            {
                this._CurrentContent = this._BarCodeText.Text;
                this._CurrentCodeType = this._CurrentBarCodeType;
            }
            else if (this._IsQrCode)
            {
                this._CurrentContent = this._QrCodeText.Text;
                this._CurrentCodeType = this._CurrentQrCodeType;
            }
            this._CodeControlRefresh();
        }
        private void _CodeControlRefresh()
        {
            if (this._CurrentContent != this._CodeControl.Text)
                this._CodeControl.Text = this._CurrentContent;

            if (this._CurrentCodeType != this._CodeControl.CodeType)
                this._CodeControl.CodeType = this._CurrentCodeType;

            if (this._CurrentLabelVisible != this._CodeControl.LabelVisible)
                this._CodeControl.LabelVisible = this._CurrentLabelVisible;
        }
        private string _CurrentContent;
        private CodeType _CurrentCodeType;
        private bool _CurrentLabelVisible;
        private bool _IsBarCode;
        private bool _IsQrCode;
        #region QR code event handlers
        private void _QControl_TimeChanged(object sender, EventArgs e)
        {
            this._StatusRefresh();
        }
        private void _QrCodeText_TextChanged(object sender, EventArgs e)
        {
            this._CurrentContent = this._QrCodeText.Text;
            this._CodeControlRefresh();
        }
        private void _CorrectLevelCombo_SelectedIndexChanged(object sender, EventArgs e)
        {
            Noris.LCS.AIDCCodes.CodeType codeType = CodeType.QrCodeMedium;
            switch (this._CorrectLevelCombo.SelectedIndex)
            {
                case 0:
                    codeType = Noris.LCS.AIDCCodes.CodeType.QrCodeLow;
                    break;
                case 1:
                    codeType = Noris.LCS.AIDCCodes.CodeType.QrCodeMedium;
                    break;
                case 2:
                    codeType = Noris.LCS.AIDCCodes.CodeType.QrCodeQuartile;
                    break;
                case 3:
                    codeType = Noris.LCS.AIDCCodes.CodeType.QrCodeHigh;
                    break;
            }
            this._CurrentQrCodeType = codeType;

            this._CurrentCodeType = codeType;
            this._CodeControlRefresh();
        }
        private Noris.LCS.AIDCCodes.CodeType _CurrentQrCodeType;
        private void _StatusPatternAll_Click(object sender, EventArgs e)
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(TestForm));
            this._StatusPatternDdb.Image = ((System.Drawing.Image)(resources.GetObject("_StatusPatternAll.Image")));
            // this._CodeControl.UseMatrixPattern = null;
        }
        private void _StatusPattern4_Click(object sender, EventArgs e)
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(TestForm));
            this._StatusPatternDdb.Image = ((System.Drawing.Image)(resources.GetObject("_StatusPattern4.Image")));
            // this._CodeControl.UseMatrixPattern = 4;
        }
        private void _StatusRefresh()
        {
            string text1 = "";
            string text2 = "";
            string text3 = "";
            if (this._IsQrCode || this._IsBarCode)
            {
                text2 = _StatusGetTimeText(this._CodeControl.LastPaintTime);
                if (this._CodeControl.Matrix != null)
                {
                    text1 = _StatusGetTimeText(this._CodeControl.LastEncodeTime);
                    text3 = this._CodeControl.Matrix.CodeVersion;
                }
            }
            this._StatusEncodeText.Text = text1;
            this._StatusPaintText.Text = text2;
            this._StatusCodeText.Text = text3;
        }
        private string _StatusGetTimeText(decimal time)
        {
            decimal microsec = Math.Round(time * 1000000,2);
            return microsec.ToString("### ##0.00").Trim() + " microsec";
        }
        #endregion
        #region QrMessages
        private void _QrMessagesInit()
        {
            this._QrMessagePhoneNumber = new QrCode.QrMessagePhoneNumber();
            this._QrMessageSmsTo = new QrCode.QrMessageSmsTo();
            this._QrMessageContactInfo = new QrCode.QrMessageContactInfo();
            this._QrMessageUrl = new QrCode.QrMessageUrl();
            this._QrMessageMailTo = new QrCode.QrMessageMailTo();
            this._QrMessageGpsLocation = new QrCode.QrMessageGpsLocation();
            this._QrMessageCzBankSpdComplete = new QrCode.QrMessageCzBankSpdComplete();
        }
        QrCode.QrMessagePhoneNumber _QrMessagePhoneNumber;
        QrCode.QrMessageSmsTo _QrMessageSmsTo;
        QrCode.QrMessageContactInfo _QrMessageContactInfo;
        QrCode.QrMessageUrl _QrMessageUrl;
        QrCode.QrMessageMailTo _QrMessageMailTo;
        QrCode.QrMessageGpsLocation _QrMessageGpsLocation;
        QrCode.QrMessageCzBankSpdComplete _QrMessageCzBankSpdComplete;
        #region TextChanged for PHONE
        private void _PhoneNumber_TextChanged(object sender, EventArgs e)
        {
            this._QrMessagePhoneNumber.PhoneNumber = _PhoneNumber.Text;
            this._QrCodeText.Text = ((QrCode.IQrMessageFormats)this._QrMessagePhoneNumber).QrMessage;
        }
        #endregion
        #region TextChanged for SMS
        private void _SmsPhoneNumber_TextChanged(object sender, EventArgs e)
        {
            this._QrMessageSmsTo.SmsNumber = _SmsPhoneNumber.Text;
            this._QrCodeText.Text = ((QrCode.IQrMessageFormats)this._QrMessageSmsTo).QrMessage;
        }
        private void _SmsContent_TextChanged(object sender, EventArgs e)
        {
            this._QrMessageSmsTo.SmsContent = _SmsContent.Text;
            this._QrCodeText.Text = ((QrCode.IQrMessageFormats)this._QrMessageSmsTo).QrMessage;
        }
        #endregion
        #region TextChanged for URL
        private void _WebUrl_TextChanged(object sender, EventArgs e)
        {
            this._QrMessageUrl.UrlAdress = _WebUrl.Text;
            this._QrCodeText.Text = ((QrCode.IQrMessageFormats)this._QrMessageUrl).QrMessage;
        }
        #endregion
        #region TextChanged for EMAIL
        private void _MailAdresss_TextChanged(object sender, EventArgs e)
        {
            this._QrMessageMailTo.MailAdress = _MailAdresss.Text;
            this._QrCodeText.Text = ((QrCode.IQrMessageFormats)this._QrMessageMailTo).QrMessage;
        }
        private void _MailSubject_TextChanged(object sender, EventArgs e)
        {
            this._QrMessageMailTo.MailSubject = _MailSubject.Text;
            this._QrCodeText.Text = ((QrCode.IQrMessageFormats)this._QrMessageMailTo).QrMessage;
        }
        private void _MailContent_TextChanged(object sender, EventArgs e)
        {
            this._QrMessageMailTo.MailContent = _MailContent.Text;
            this._QrCodeText.Text = ((QrCode.IQrMessageFormats)this._QrMessageMailTo).QrMessage;
        }
        #endregion
        #region TextChanged for CONTACT
        private void _ContactFirstName_TextChanged(object sender, EventArgs e)
        {
            this._QrMessageContactInfo.FirstName = _ContactFirstName.Text;
            this._QrCodeText.Text = ((QrCode.IQrMessageFormats)this._QrMessageContactInfo).QrMessage;
        }
        private void _ContactLastName_TextChanged(object sender, EventArgs e)
        {
            this._QrMessageContactInfo.LastName = _ContactLastName.Text;
            this._QrCodeText.Text = ((QrCode.IQrMessageFormats)this._QrMessageContactInfo).QrMessage;
        }
        private void _ContactAdressLine1_TextChanged(object sender, EventArgs e)
        {
            this._QrMessageContactInfo.AddressLine1 = _ContactAdressLine1.Text;
            this._QrCodeText.Text = ((QrCode.IQrMessageFormats)this._QrMessageContactInfo).QrMessage;
        }
        private void _ContactAdressLine2_TextChanged(object sender, EventArgs e)
        {
            this._QrMessageContactInfo.AddressLine2 = _ContactAdressLine2.Text;
            this._QrCodeText.Text = ((QrCode.IQrMessageFormats)this._QrMessageContactInfo).QrMessage;
        }
        private void _ContactAdressCity_TextChanged(object sender, EventArgs e)
        {
            this._QrMessageContactInfo.City = _ContactAdressCity.Text;
            this._QrCodeText.Text = ((QrCode.IQrMessageFormats)this._QrMessageContactInfo).QrMessage;
        }
        private void _ContactAdressPostCode_TextChanged(object sender, EventArgs e)
        {
            this._QrMessageContactInfo.PostCode = _ContactAdressPostCode.Text;
            this._QrCodeText.Text = ((QrCode.IQrMessageFormats)this._QrMessageContactInfo).QrMessage;
        }
        private void _ContactPhone1_TextChanged(object sender, EventArgs e)
        {
            this._QrMessageContactInfo.PhoneNumber1 = _ContactPhone1.Text;
            this._QrCodeText.Text = ((QrCode.IQrMessageFormats)this._QrMessageContactInfo).QrMessage;
        }
        private void _ContactPhone2_TextChanged(object sender, EventArgs e)
        {
            this._QrMessageContactInfo.PhoneNumber2 = _ContactPhone2.Text;
            this._QrCodeText.Text = ((QrCode.IQrMessageFormats)this._QrMessageContactInfo).QrMessage;
        }
        private void _ContactPhone3_TextChanged(object sender, EventArgs e)
        {
            this._QrMessageContactInfo.PhoneNumber3 = _ContactPhone3.Text;
            this._QrCodeText.Text = ((QrCode.IQrMessageFormats)this._QrMessageContactInfo).QrMessage;
        }
        private void _ContactEMail_TextChanged(object sender, EventArgs e)
        {
            this._QrMessageContactInfo.EMail = _ContactEMail.Text;
            this._QrCodeText.Text = ((QrCode.IQrMessageFormats)this._QrMessageContactInfo).QrMessage;
        }
        #endregion
        #region TextChanged for SPD BANK
        private void _SpdSAccountPrefix_TextChanged(object sender, EventArgs e)
        {
            this._QrMessageCzBankSpdComplete.SAccountPrefix = _SpdSAccountPrefix.Text;
            this._SpdQrMessageIbanRefresh();
        }
        private void _SpdSAccountNumber_TextChanged(object sender, EventArgs e)
        {
            this._QrMessageCzBankSpdComplete.SAccountNumber = _SpdSAccountNumber.Text;
            this._SpdQrMessageIbanRefresh();
        }
        private void _SpdSBankCode_TextChanged(object sender, EventArgs e)
        {
            this._QrMessageCzBankSpdComplete.SBankCode = _SpdSBankCode.Text;
            this._SpdQrMessageIbanRefresh();
        }
        private void _SpdAccountIban_TextChanged(object sender, EventArgs e)
        {
            this._QrMessageCzBankSpdComplete.AccountIBAN = _SpdAccountIban.Text;
            this._SpdQrMessageRefresh();
        }
        private void _SpdAmount_TextChanged(object sender, EventArgs e)
        {
            decimal value;
            if (!String.IsNullOrEmpty(_SpdAmount.Text) && Decimal.TryParse(_SpdAmount.Text, out value))
                this._QrMessageCzBankSpdComplete.Amount = value;
            else
                this._QrMessageCzBankSpdComplete.Amount = null;
            this._SpdQrMessageRefresh();
        }
        private void _SpdCurrencyCode_TextChanged(object sender, EventArgs e)
        {
            this._QrMessageCzBankSpdComplete.CurrencyCode = _SpdCurrencyCode.Text;
            this._SpdQrMessageRefresh();
        }
        private void _SpdDueDate_TextChanged(object sender, EventArgs e)
        {
            DateTime date = DateTime.MinValue;
            string text = _SpdDueDate.Text;
            bool ok = false;
            if (!string.IsNullOrEmpty(text))
            {
                ok = DateTime.TryParse(text, out date);
                if (!ok)
                {
                    System.Globalization.DateTimeFormatInfo dfi = new System.Globalization.DateTimeFormatInfo();
                    dfi.DateSeparator = ".";
                    string[] formats = new string[] { "d.M.yy", "yyyy-MM-dd", "dd.MM.yyyy" };
                    ok = DateTime.TryParseExact(text, formats, dfi, System.Globalization.DateTimeStyles.AllowWhiteSpaces, out date);
                }
            }
            if (ok)
                this._QrMessageCzBankSpdComplete.DueDate = date;
            else
                this._QrMessageCzBankSpdComplete.DueDate = null;

            this._SpdQrMessageRefresh();
        }
        private void _SpdVarSymb_TextChanged(object sender, EventArgs e)
        {
            this._QrMessageCzBankSpdComplete.VariableSymbol = _SpdVarSymb.Text;
            this._SpdQrMessageRefresh();
        }
        private void _SpdSpecSymb_TextChanged(object sender, EventArgs e)
        {
            this._QrMessageCzBankSpdComplete.SpecificSymbol = _SpdSpecSymb.Text;
            this._SpdQrMessageRefresh();
        }
        private void _SpdConstSymb_TextChanged(object sender, EventArgs e)
        {
            this._QrMessageCzBankSpdComplete.ConstantSymbol = _SpdConstSymb.Text;
            this._SpdQrMessageRefresh();
        }
        private void _SpdMessage_TextChanged(object sender, EventArgs e)
        {
            this._QrMessageCzBankSpdComplete.Message = _SpdMessage.Text;
            this._SpdQrMessageRefresh();
        }
        private void _SpdRecIdent_TextChanged(object sender, EventArgs e)
        {
            this._QrMessageCzBankSpdComplete.RecipientIdent = _SpdRecIdent.Text;
            this._SpdQrMessageRefresh();
        }
        private void _SpdRecName_TextChanged(object sender, EventArgs e)
        {
            this._QrMessageCzBankSpdComplete.RecipientName = _SpdRecName.Text;
            this._SpdQrMessageRefresh();
        }
        private void _SpdPayIdent_TextChanged(object sender, EventArgs e)
        {
            this._QrMessageCzBankSpdComplete.PayerIdent = _SpdPayIdent.Text;
            this._SpdQrMessageRefresh();
        }
        private void _SpdPayType_TextChanged(object sender, EventArgs e)
        {
            this._QrMessageCzBankSpdComplete.PaymentType = _SpdPayType.Text;
            this._SpdQrMessageRefresh();
        }
        private void _SpdQrMessageIbanRefresh()
        {
            if (this._QrMessageCzBankSpdComplete.AccountIBAN != null)
                this._SpdAccountIban.Text = this._QrMessageCzBankSpdComplete.AccountIBAN;
        }
        private void _SpdQrMessageRefresh()
        {
            try
            {
                this._QrCodeText.Text = ((QrCode.IQrMessageFormats)this._QrMessageCzBankSpdComplete).QrMessage;
            }
            catch (Exception exc)
            {
                MessageBox.Show("Chyba: " + exc.Message, "SPD format", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }
        #endregion
        #endregion
        #region BarCode event handlers
        private void _InitBarCodeTypes()
        {
            Point origin = new Point(17, 9);
            Size size = new Size(140, 22);
            Size shift = new Size(size.Width + 6, size.Height + 3);
            int max = this._BarCodeTypePanel.ClientSize.Width - origin.X;

            Point current = origin;
            int tabIndex = 0;
            foreach (object item in Enum.GetValues(typeof(Noris.LCS.AIDCCodes.CodeType)))
            {
                CodeType codeType = (CodeType)item;
                if (codeType == CodeType.None || codeType == CodeType.QrCode || codeType == CodeType.QrCodeLow || codeType == CodeType.QrCodeMedium || codeType == CodeType.QrCodeQuartile || codeType == CodeType.QrCodeHigh) continue;

                RadioButton rb = new RadioButton() { AutoSize = false, TabStop = true, TabIndex = tabIndex++, Location = current, Size = size, Tag = codeType, UseVisualStyleBackColor = true, Name = "CodeType_" + codeType.ToString(), Text = codeType.ToString() };
                this._BarCodeTypePanel.Controls.Add(rb);

                rb.Enabled = CodeControl.CanProcessCodeType(codeType);         // true if CodeType can be processed
                rb.Checked = (rb.Enabled && codeType == CodeType.EAN13);       // EAN13 is initially checked
                rb.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.0F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(238)));
                rb.Click += new EventHandler(_CodeType_Click);
                this._ToolTip.SetToolTip(rb, codeType.ToString());

                current = new Point(current.X + shift.Width, current.Y);
                if (current.X + size.Width > max)
                    current = new Point(origin.X, current.Y + shift.Height);
            }
        }
        private void _BarCodeContent_TextChanged(object sender, EventArgs e)
        {
            if (!this._IsBarCode) return;
            this._CurrentContent = this._BarCodeText.Text;
            this._CodeControlRefresh();
        }
        private void _CodeType_Click(object sender, EventArgs e)
        {
            if (!this._IsBarCode) return;
            Control rb = sender as Control;
            if (rb == null) return;
            string text = rb.Text;

            CodeType codeType;
            if (!Enum.TryParse<CodeType>(text, true, out codeType)) return;

            this._CurrentBarCodeType = codeType;

            this._CurrentCodeType = codeType;
            this._CodeControlRefresh();
        }
        private CodeType _CurrentBarCodeType;
        private void _BarControl_TimeChanged(object sender, EventArgs e)
        {
            this._StatusRefresh();
        }
        #endregion
        #region Toolbar
        private void _ToolCopyBtn_Click(object sender, EventArgs e)
        {
            this._CopyToClipboard();
        }
        private void _ToolSaveAsBtn_Click(object sender, EventArgs e)
        {
            this._CopyToFile();
        }
        private void _ToolLabelBtn_Click(object sender, EventArgs e)
        {
            this._CurrentLabelVisible = this._ToolLabelBtn.Checked;
            this._CodeControlRefresh();
        }
        private void _CopyToClipboard()
        {
            try
            {
                CodeProperties properties = _CreateCodeProperties();
                Bitmap bitmap = CodeManager.EncodeToBitmap(properties);
                if (bitmap != null)
                {
                    System.Windows.Forms.Clipboard.SetImage(bitmap);
                    MessageBox.Show("Obrázek je uložen v clipboardu. Ctrl+V jej vloží do textového editoru.", "Copy...", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("Došlo k chybě, obrázek nebyl vytvořen.", "Copy...", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception exc)
            {
                MessageBox.Show("Došlo k chybě:\r\n" + exc.Message, "Copy...", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally { }
        }
        private void _CopyToFile()
        {
            try
            {
                string name = _GetFileNameFrom(this._CurrentContent);
                SaveFileDialog sfd = new SaveFileDialog() { AddExtension = true, AutoUpgradeEnabled = true, FileName = name, InitialDirectory = this._SaveAsDir, OverwritePrompt = true, RestoreDirectory = true, SupportMultiDottedExtensions = true, Title = "Save to file..." };
                sfd.Filter = "GIF|*.gif|JPEG|*.jpeg|PNF|*.png|TIFF|*.tif|BMP|*.bmp|All files|*.*";   // "BMP|*.bmp|EMF|*.emf|EXIF|*.exif|GIF|*.gif|ICO|*.ico|JPEG|*.jpeg|PNF|*.png|TIFF|*.tif|WMF|*.wmf|All files|*.*";

                var dialogResult = sfd.ShowDialog(this);
                if (dialogResult != System.Windows.Forms.DialogResult.OK) return;
                string targetFile = sfd.FileName;

                CodeProperties properties = _CreateCodeProperties();
                string encodingResult = CodeManager.EncodeToFile(properties, targetFile);
                if (encodingResult == null)
                {
                    this._SaveAsDir = System.IO.Path.GetDirectoryName(targetFile);
                    MessageBox.Show("Obrázek je uložen v souboru:\r\n" + targetFile, "Save as...", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("Došlo k chybě:\r\n" + encodingResult, "Save as...", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception exc)
            {
                MessageBox.Show("Došlo k chybě:\r\n" + exc.Message, "Save as...", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally { }
        }

        private string _GetFileNameFrom(string content)
        {
            string name = "";
            string exclude = "\r\n\t:\\/?*+-|;°ˇ%#&@{}€ß§´=";
            foreach (char c in content)
            {
                if (!exclude.Contains(c))
                    name += c.ToString();
            }
            return name;
        }
        private CodeProperties _CreateCodeProperties()
        {
            string content = this._CurrentContent;
            CodeType codeType = this._CurrentCodeType;
            bool drawLabel = this._CurrentLabelVisible;
            int moduleSize = this._ModuleSize;

            CodeProperties properties = new CodeProperties() { Content = content, CodeType = codeType, ModuleSize = moduleSize, LabelVisible = drawLabel };
            return properties;
        }
        private int _ModuleSize
        {
            get
            {
                string text = this._ToolSizeText.Text;
                int size;
                if (Int32.TryParse(text, out size))
                {
                    if (size < 0) return 0;
                    if (size < 64) return size;
                }
                return 6;
            }
            set
            {
                this._ToolSizeText.Text = value.ToString();
            }
        }
        private string _SaveAsDir;
        #endregion
    }
}
